package interview;

public class interview_16_15 {
    public int[] masterMind(String solution, String guess) {
        boolean[] flag = new boolean[4];
        int[] result = new int[2];
        for (int i = 0; i < 4; ++i) {
            if (solution.charAt(i) == guess.charAt(i)) {
                result[0] += 1;
                flag[i] = true;
            }
        }
        int[] color = new int[4];
        // RYGB
        for (int i = 0; i < 4; ++i) {
            if(flag[i]) continue;
            if (guess.charAt(i) == 'R') {
                color[0] += 1;
            } else if (guess.charAt(i) == 'Y') {
                color[1] += 1;
            } else if (guess.charAt(i) == 'G') {
                color[2] += 1;
            } else {
                color[3] += 1;
            }
        }
        for (int i = 0; i < 4; ++i) {
            if(flag[i]) continue;
            if (solution.charAt(i) == 'R' && color[0] > 0) {
                color[0] -= 1;
                result[1] += 1;
            } else if (solution.charAt(i) == 'Y' && color[1] > 0) {
                color[1] -= 1;
                result[1] += 1;
            } else if (solution.charAt(i) == 'G' && color[2] > 0) {
                color[2] -= 1;
                result[1] += 1;
            } else if (solution.charAt(i)=='B'&&color[3]>0) {
                color[3]-=1;
                result[1]+=1;
            }
        }
        return result;
    }
}
